/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.crawwle.career.crawler.controller;

import java.io.FileInputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.couchbase.client.CouchbaseClient;

import edu.uci.ics.crawler4j.crawler.CrawlConfig;
import edu.uci.ics.crawler4j.crawler.CrawlController;
import edu.uci.ics.crawler4j.crawler.WebCrawler;
import edu.uci.ics.crawler4j.fetcher.PageFetcher;
import edu.uci.ics.crawler4j.robotstxt.RobotstxtServer;

/**
 * The controller that manages a crawling session. This class creates the
 * crawler threads and monitors their progress.
 *
 * @author jianwei.chen
 */
public class CareerController extends CrawlController {

	static final Logger logger = Logger.getLogger(CareerController.class.getName());

	protected  CouchbaseClient couchbaseClient;

	public  Map<String,String> urlMap;



	/**
	 * @return urlMap
	 */
	public Map<String, String> getUrlMap() {
		return urlMap;
	}

	/**
	 * @param urlMap セットする urlMap
	 */
	public void setUrlMap(Map<String, String> urlMap) {
		this.urlMap = urlMap;
	}

	public CareerController(CrawlConfig config, PageFetcher pageFetcher, RobotstxtServer robotstxtServer)
			throws Exception {
		super(config,pageFetcher,robotstxtServer);

		logger.info("Startting CouchbaseClient...");

		Properties conf = new Properties();
        conf.load(new FileInputStream("/opt/conf/crawwle.properties"));

		ArrayList<URI> nodes = new ArrayList<URI>();
		nodes.add(URI.create(conf.getProperty("coubase.server.node.url")));
		couchbaseClient = new CouchbaseClient(nodes, conf.getProperty("coubase.server.bucket.career"), "");
	}


	protected <T extends WebCrawler> void start(final Class<T> _c, final int numberOfCrawlers, boolean isBlocking) {
		super.start(_c,numberOfCrawlers,isBlocking);


		logger.info("Closed CouchbaseClient...");
		if(this.isFinished()){
			this.shutdown();
		}
	}



	/**
	 * @return couchbaseClient
	 */
	public CouchbaseClient getCouchbaseClient() {
		return couchbaseClient;
	}

	/**
	 * @param couchbaseClient セットする couchbaseClient
	 */
	public void setCouchbaseClient(CouchbaseClient couchbaseClient) {
		this.couchbaseClient = couchbaseClient;
	}

	/**
	 * Set the current crawling session set to 'shutdown'. Crawler threads
	 * monitor the shutdown flag and when it is set to true, they will no longer
	 * process new pages.
	 */
	public void shutdown() {
		logger.info("Shutting down...");
		super.shutdown();
		this.couchbaseClient.shutdown();
	}
}
